package ncut.shumei.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.Collections;
import java.util.List;

@Configuration
public class SwaggerConfig {
    
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .securityContexts(Collections.singletonList(securityContext()))
                .securitySchemes(Collections.singletonList(apiKey()))
                .select()
                .apis(RequestHandlerSelectors.basePackage("ncut.shumei.controller"))
                .paths(PathSelectors.any())
                .build()
                .tags(
                    new Tag("认证管理", "包含登录、注册、token验证等接口"),
                    new Tag("用户管理", "用户的增删改查、状态管理等接口"),
                    new Tag("电梯管理", "电梯的增删改查、状态管理等接口"),
                    new Tag("维护任务", "维护任务的创建、分配、状态更新等接口"),
                    new Tag("文档管理", "API文档的下载等接口")
                );
    }
    
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("电梯维护管理系统 API文档")
                .description("本文档详细说明了电梯维护管理系统的所有API接口规范，包括：\n\n" +
                        "1. 认证管理：用户登录、注册、token验证\n" +
                        "2. 用户管理：用户信息的增删改查\n" +
                        "3. 电梯管理：电梯信息的增删改查\n" +
                        "4. 维护任务：任务的创建、分配、状态更新\n" +
                        "5. 文档管理：API文档下载")
                .version("1.0")
                .contact(new Contact("开发团队", "http://example.com", "support@example.com"))
                .license("MIT License")
                .licenseUrl("https://opensource.org/licenses/MIT")
                .build();
    }
    
    private ApiKey apiKey() {
        return new ApiKey("JWT", "Authorization", "header");
    }
    
    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .build();
    }
    
    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Collections.singletonList(new SecurityReference("JWT", authorizationScopes));
    }
} 